import sys
import urllib

sys.path.extend(["F:/ML/BNC", "D:/BNC", "/www/wwwroot/BNC"])

import demjson
import pymysql
import requests
from DataBase import DatabaseConsts
from Utils.Functions import CookieObject

host = '127.0.0.1'
port = 3306
db = DatabaseConsts.StockOneDayKLineDB
user = 'root'
password = 'h303567469'

connection = pymysql.connect(host=host,
                             port=port,
                             user=user,
                             password=password,
                             db=db,
                             charset='utf8')

curr = connection.cursor()

if __name__ == '__main__':
    # 获取需要改变的标签
    # 从同花顺查询是否包含对应的标签，如果包含多个需要输入索引，或者自己输入，如果不存在就自己输入
    cookie_url = 'http://www.iwencai.com/stockpick/search?typed=0&preParams=&ts=1&f=1&qs=result_original&selfsectsn=&querytype=stock&searchfilter=&tid=stockpick&w=%E5%8C%BB%E4%BF%9D%E7%9B%AE%E5%BD%95&queryarea='
    cookie_manager = CookieObject(url=cookie_url)
    if cookie_manager.get_cookie() is None:
        cookie_manager.refresh_cookie()

    headers = dict()

    renamed_dic = dict()
    not_renamed_list = list()
    not_normaled_list = list()

    url_format = 'http://www.iwencai.com/stockpick/load-data?typed=0&preParams=&ts=1&f=1&qs=result_original&selfsectsn=&querytype=stock&searchfilter=&tid=stockpick&w={}&queryarea='

    select_need_named_tag = "select distinct(tag) from code_tag_date"
    curr.execute(select_need_named_tag)
    res = curr.fetchall()
    tag_list = [r[0] for r in res]

    times = 10
    # fixme 全量
    tag_list = tag_list[:3000]

    for tag_rename in tag_list:
        if len(tag_rename)  > 9:
            not_normaled_list.append(tag_rename)
            continue
        print("parse tag:{}".format(tag_rename))
        if not times:
            times = 10
            cookie_manager.refresh_cookie()
            headers = {
                'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
                'Accept-Encoding': 'gzip, deflate',
                'Accept-Language': 'zh-CN,zh;q=0.9',
                'Cache-Control': 'max-age=0',
                'Connection': 'keep-alive',
                'hexin-v': cookie_manager.get_cookie_item('v'),
                'Cookie': cookie_manager.get_cookie_value(),
                'Referer': 'http://www.iwencai.com/stockpick/search?typed=1&preParams=&ts=1&f=1&qs=index_rewrite&selfsectsn=&querytype=stock&searchfilter=&tid=stockpick&w=%E4%BB%8A%E6%97%A5%E5%85%AC%E5%91%8A%E5%88%A9%E7%A9%BA&perpage=200',
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36',
                'X-Requested-With': 'XMLHttpRequest'
            }

        times -= 1

        # 获取同花顺的结果
        geted_data = False
        json_di = {}
        try:
            _url = url_format.format(urllib.parse.quote(tag_rename))
            json_st = requests.get(_url, headers=headers).text
            json_di = demjson.decode(json_st)
            geted_data = json_di['success']
        except Exception as e:
            not_renamed_list.append(tag_rename)
            print("调用失败~")
            continue

        tag_advices = list()
        if geted_data:
            try:
                tag_advices = json_di['data']['result']['filterInfo']['4']['filterValue']
                print("rename advices:{}".format(tag_advices))
            except Exception as e:
                print("not advices of {}".format(tag_rename))
                not_renamed_list.append(tag_rename)
                continue

        if geted_data:
            renamed_tag = tag_advices
            renamed_dic[tag_rename] = renamed_tag
        else:
            print("throw renamed tag :{}".format(tag_rename))
            not_renamed_list.append(tag_rename)
    with open("a.log",'a', encoding='utf8') as f:
        f.write("renamed tag:{} \n not_renamed_:{}\n长度不符合的标签:{}\n\n".format(renamed_dic, not_renamed_list, not_normaled_list))
